<?php

require_once(APPPATH.'models/domain/Entity.php');

class MY_Model extends Model{

	var $TABLE_NAME;
	var $TABLE_PK;
	var $DATE_FORMAT='Y-m-d H:i:s';

	function MY_Model($table_name,$table_pk){
		parent::Model();
 		$this->TABLE_NAME	= TABLE_PREFIX . $table_name;
		$this->TABLE_PK		= $table_pk;
	}

	/*****************************************************
	 * ABSTRACTS
	 *****************************************************/
	protected function afromEntityToDb(Entity $e){}

	protected function afromDbToEntity(array $p_e){}

	/*****************************************************
	 * UTILS PROTECTED
	 *****************************************************/
	protected function fromArrayDbToArrayEntity(array $resultset){
		$ret = array();
		foreach ($resultset as $ae){
			$ret[] = $this->afromDbToEntity($ae);
		}
		return $ret;
	}

	protected function fromEntityToDb(Entity $e){
		if($e==NULL)return NULL;
		$ret = $this->afromEntityToDb($e);
		foreach($ret as $key => $val){
			if($val===NULL){
				unset($ret[$key]);
			}
		}
		return $ret;
	}

	protected function fromDbToEntity($result_array){
		if(count($result_array)==0)return NULL;
		return ($result_array[0]==NULL) ? NULL:$this->afromDbToEntity($result_array[0]);
	}

	protected function getDateToDb(DateTime $date){
		return !isset($date)||$date==NULL ? NULL:$date->format($this->DATE_FORMAT);
	}

	protected function getDateFromDb($dbdate){
		return date_create_from_format($this->DATE_FORMAT, $dbdate);
	}

	protected function getByWhat($key,$val){
		$this->db->where($key,$val);
		$query = $this->db->get($this->TABLE_NAME,1);
		return $this->fromDbToEntity($query->result_array());
	}

	/*****************************************************
	 * UTILS PUBLIC
	 *****************************************************/
	public function getById($id)
	{
		return $this->getByWhat($this->TABLE_PK,$id);
	}

	public function deleteById($id)
	{
		$this->db->delete($this->TABLE_NAME, array($this->TABLE_PK => $id));
		if ($this->db->trans_status() === FALSE){
			throw new Exception("Ha ocurrido un error al tratar de eliminar la entidad");
		}
	}

	public function create(Entity $e)
	{
		$ae = $this->fromEntityToDb($e);
		$this->db->insert($this->TABLE_NAME, $ae);
		if ($this->db->trans_status() === FALSE){
			throw new Exception("Ha ocurrido un error al tratar de crear la entidad: " . $this->db->_error_message());
		}
		return $this->db->insert_id();
	}

	public function delete(Entity $e)
	{
		$ae = $this->fromEntityToDb($e);
		$this->db->where($this->TABLE_PK,$ae[$this->TABLE_PK]);
		$this->db->delete($this->TABLE_NAME);
		if ($this->db->trans_status() === FALSE){
			throw new Exception("Ha ocurrido un error al tratar de eliminar la entidad");
		}
	}

	public function update(Entity $e)
	{
		$ae = $this->fromEntityToDb($e);
		$this->db->where($this->TABLE_PK,$ae[$this->TABLE_PK]);
		unset($ae[$this->TABLE_PK]);
		$this->db->update($this->TABLE_NAME,$ae);
		
		if ($this->db->trans_status() === FALSE){
			throw new Exception("Ha ocurrido un error al tratar de modificar la entidad");
		}
	}

}
?>
